{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vector Fitting\n",
    "This is a brief introduction to Vector Fitting. The concept and its algorithm was proposed in 1999 by Bjørn Gustavsen and Adam Semlyen [[1](#link_ref1)]. See the Vector Fitting website for more information [[2](#link_ref2)]. \n",
    "The main application of Vector Fitting is to model the original sampled frequency responses of an active or passive device in a circuit simulator. Also see the [API documentation](../api/vectorFitting.html) and the [application examples](../examples/index.html#vector-fitting) for more information about the implementation in scikit-rf."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mathematical Description\n",
    "The idea of Vector Fitting is to fit a set of rational model functions to a set of sampled frequency responses $\\mathbf{\\underline{H}}_\\mathrm{sampled}$, for example from a [S](https://en.wikipedia.org/wiki/Scattering_parameters), [Y](https://en.wikipedia.org/wiki/Admittance_parameters) or [Z](https://en.wikipedia.org/wiki/Impedance_parameters) matrix. The model function $\\mathbf{\\underline{H}}(\\mathrm{\\underline{s}})$ is defined in the Laplace domain with $\\mathrm{\\underline{s}} = \\sigma + \\mathrm{j} \\omega$:\n",
    "\\begin{equation}\n",
    "\\mathbf{\\underline{H}}(\\mathrm{\\underline{s}}) = \\mathbf{d} + \\mathrm{\\underline{s}} \\mathbf{e} + \\sum_{k=1}^K \\frac{\\underline{\\mathbf{c}}_{k}}{\\mathrm{\\underline{s}}-\\underline{p}_k}\n",
    "\\end{equation}\n",
    "\n",
    "For the desired fit, that model function shall match the given frequency responses at each of its frequency samples $\\omega_n$:\n",
    "\\begin{equation}\n",
    "\\mathbf{\\underline{H}}(\\mathrm{\\underline{s}} = \\mathrm{j} \\omega_n) \\overset{!}{=} \\mathbf{\\underline{H}}_\\mathrm{sampled}(\\omega_n)\n",
    "\\end{equation}\n",
    "\n",
    "Generally, $\\mathbf{\\underline{H}}(\\mathrm{\\underline{s}})$ is a vector holding the individual complex frequency responses $\\underline{H}_1(\\mathrm{\\underline{s}})$, $\\underline{H}_2(\\mathrm{\\underline{s}})$, ..., $\\underline{H}_M(\\mathrm{\\underline{s}})$ of the model. All elements in $\\mathbf{\\underline{H}}(\\mathrm{\\underline{s}})$ share a common set of complex poles $\\underline{p}_k$, but have individual sets of complex residues $\\mathbf{\\underline{c}}_k$, real constants $\\mathbf{d}$  and real proportional coefficients $\\mathbf{e}$, which are therefore vectors as well:\n",
    "\\begin{equation}\n",
    "\\mathbf{\\underline{p}} = \\begin{pmatrix} \\underline{p}_1 & \\underline{p}_2 & \\underline{p}_3 & \\cdots & \\underline{p}_K \\end{pmatrix}\n",
    "\\end{equation}\n",
    "\n",
    "\\begin{equation}\n",
    "\\mathbf{\\underline{c}} = \\begin{pmatrix} \n",
    "\\underline{c}_{1,1} & \\underline{c}_{2,1} & \\underline{c}_{3,1} & \\cdots & \\underline{c}_{K,1} \\\\\n",
    "\\underline{c}_{1,2} & \\underline{c}_{2,2} & \\underline{c}_{3,2} & \\cdots & \\underline{c}_{K,2} \\\\\n",
    "\\vdots\\\\\n",
    "\\underline{c}_{1,M} & \\underline{c}_{2,M} & \\underline{c}_{3,M} & \\cdots & \\underline{c}_{K,M} \\\\\n",
    "\\end{pmatrix}\n",
    "\\end{equation}\n",
    "\n",
    "\\begin{equation}\n",
    "\\mathbf{d} = \\begin{pmatrix} d_1 \\\\ d_2 \\\\ \\vdots \\\\ d_M \\end{pmatrix}\n",
    "\\end{equation}\n",
    "\n",
    "\\begin{equation}\n",
    "\\mathbf{e} = \\begin{pmatrix} e_1 \\\\ e_2 \\\\ \\vdots \\\\ e_M \\end{pmatrix}\n",
    "\\end{equation}\n",
    "\n",
    "The number of poles, $K$, required for a good fit of $\\mathbf{\\underline{H}}(\\mathrm{\\underline{s}})$ to $\\mathbf{\\underline{H}}_\\mathrm{sampled}$ depends on the shape of the responses.\n",
    "\n",
    "As an example, the goal could be to fit the rational model function to the S matrix of a 2-port ($M = 4$) sampled at $N$ frequencies $\\omega_n$:\n",
    "\n",
    "\\begin{equation}\n",
    "\\begin{pmatrix} \n",
    "\\underline{S}_{11}(\\omega_1) \\\\\n",
    "\\underline{S}_{12}(\\omega_1) \\\\\n",
    "\\underline{S}_{21}(\\omega_1) \\\\\n",
    "\\underline{S}_{22}(\\omega_1) \\\\\n",
    "\\vdots \\\\\n",
    "\\underline{S}_{11}(\\omega_\\mathrm{N}) \\\\\n",
    "\\underline{S}_{12}(\\omega_\\mathrm{N}) \\\\\n",
    "\\underline{S}_{21}(\\omega_\\mathrm{N}) \\\\\n",
    "\\underline{S}_{22}(\\omega_\\mathrm{N})\n",
    "\\end{pmatrix}\n",
    "\\overset{!}{=}\n",
    "\\begin{pmatrix}\n",
    "d_{11} + \\mathrm{j} \\omega_1 e_{11} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,11}}{\\mathrm{j} \\omega_1 - \\underline{p}_k}\n",
    "\\\\\n",
    "d_{12} + \\mathrm{j} \\omega_1 e_{12} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,12}}{\\mathrm{j} \\omega_1 - \\underline{p}_k}\n",
    "\\\\\n",
    "d_{21} + \\mathrm{j} \\omega_1 e_{21} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,21}}{\\mathrm{j} \\omega_1 - \\underline{p}_k}\n",
    "\\\\\n",
    "d_{22} + \\mathrm{j} \\omega_1 e_{22} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,22}}{\\mathrm{j} \\omega_1 - \\underline{p}_k}\n",
    "\\\\\n",
    "\\vdots\n",
    "\\\\\n",
    "d_{11} + \\mathrm{j} \\omega_\\mathrm{N} e_{11} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,11}}{\\mathrm{j} \\omega_\\mathrm{N} - \\underline{p}_k}\n",
    "\\\\\n",
    "d_{12} + \\mathrm{j} \\omega_\\mathrm{N} e_{12} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,12}}{\\mathrm{j} \\omega_\\mathrm{N} - \\underline{p}_k}\n",
    "\\\\\n",
    "d_{21} + \\mathrm{j} \\omega_\\mathrm{N} e_{21} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,21}}{\\mathrm{j} \\omega_\\mathrm{N} - \\underline{p}_k}\n",
    "\\\\\n",
    "d_{22} + \\mathrm{j} \\omega_\\mathrm{N} e_{22} + \\sum_{k=1}^K \\frac{\\underline{c}_{k,22}}{\\mathrm{j} \\omega_\\mathrm{N} - \\underline{p}_k}\n",
    "\\end{pmatrix}\n",
    "\\end{equation}\n",
    "\n",
    "During the Vector Fitting process, the model parameters $\\mathbf{\\underline{p}}$, $\\mathbf{\\underline{c}}$, $\\mathbf{d}$ and $\\mathbf{e}$ will be optimized in an iterative manner until a good fit is achieved."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Equivalent Electrical Circuits\n",
    "The benefit of Vector Fitting the sampled frequency responses is the easy representation of the rational basis function with equivalent electrical circuits. A detailed derivation can be found in [[3](#link_ref3)].\n",
    "\n",
    "### Constant and Proportional Term\n",
    "The constant and proportional terms $\\mathbf{d} + \\mathrm{\\underline{s}} \\mathbf{e}$ can be represented in an electrical circuit by equivalent impedances $\\underline{Z}_\\mathrm{RL}(\\mathrm{\\underline{s}})$ or equivalent admittances $\\underline{Y}_\\mathrm{RC}(\\mathrm{\\underline{s}})$ build out of a series RL or parallel RC circuits.\n",
    "\n",
    "<img src='./figures/circuit_series_RL.svg' width='170px'>\n",
    "\n",
    "<img src='./figures/circuit_parallel_RC.svg' width='150px'>\n",
    "\n",
    "Target response of the constant and proportional term:\n",
    "\\begin{equation}\n",
    "\\underline{H}_\\mathrm{target}(\\mathrm{\\underline{s}}) = d_i + \\mathrm{\\underline{s}} e_i\n",
    "\\end{equation}\n",
    "\n",
    "#### Impedance of a series RL circuit:\n",
    "\\begin{equation}\n",
    "\\underline{Z}_\\mathrm{RL}(\\mathrm{\\underline{s}}) = R + \\mathrm{\\underline{s}} L\n",
    "\\end{equation}\n",
    "This impedance matches the target response if $R = d_i$ and $L = e_i$.\n",
    "\n",
    "#### Admittance of a parallel RC circuit: \n",
    "\\begin{equation}\n",
    "\\underline{Y}_\\mathrm{RC}(\\mathrm{\\underline{s}}) = \\frac{1}{R} + \\mathrm{\\underline{s}} C\n",
    "\\end{equation}\n",
    "This admittance matches the target response if $R = \\frac{1}{d_i}$ and $C = e_i$.\n",
    "\n",
    "### Real Poles and Residues\n",
    "The individual terms $\\frac{c_{k,i}}{\\mathrm{\\underline{s}} - p_{k,i}}$ of the fit with a real pole $p_{k,i}$ and a real residue $c_{k,i}$ can be represented in an electrical circuit by equivalent impedances $\\underline{Z}_\\mathrm{RC}(\\mathrm{\\underline{s}})$ or equivalent admittances $\\underline{Y}_\\mathrm{RL}(\\mathrm{\\underline{s}})$ build out of a parallel RC or series RL circuits.\n",
    "\n",
    "Target response of the real pole-residue term:\n",
    "\\begin{equation}\n",
    "\\underline{H}_\\mathrm{target}(\\mathrm{\\underline{s}}) = \\frac{c_{k,i}}{\\mathrm{\\underline{s}} - p_{k,i}}\n",
    "\\end{equation}\n",
    "\n",
    "#### Impedance of a parallel RC circuit: \n",
    "The parallel RC circuit is the same as above for the constant and proportional term, but this time its impedance $\\underline{Z}_\\mathrm{RC}(\\mathrm{\\underline{s}})$ is used instead of its admittance:\n",
    "\\begin{equation}\n",
    "\\underline{Z}_\\mathrm{RC}(\\mathrm{\\underline{s}}) = \\frac{\\frac{1}{C}}{\\mathrm{\\underline{s}} + \\frac{1}{RC}}\n",
    "\\end{equation}\n",
    "This impedance matches the target response if $C = \\frac{1}{c_{k,i}}$ and $R = \\frac{c_{k,i}}{-p_{k,i}}$.\n",
    "\n",
    "#### Admittance of a series RL circuit: \n",
    "The series RL circuit is the same as above for the constant and proportional term, but this time its admittance $\\underline{Y}_\\mathrm{RL}(\\mathrm{\\underline{s}})$ is used instead of its impedance:\n",
    "\\begin{equation}\n",
    "\\underline{Y}_\\mathrm{RL}(\\mathrm{\\underline{s}}) = \\frac{\\frac{1}{L}}{\\mathrm{\\underline{s}} + \\frac{R}{L}}\n",
    "\\end{equation}\n",
    "This admittance matches the target response if $L = \\frac{1}{c_{k,i}}$ and $R = \\frac{-p_{k,i}}{c_{k,i}}$.\n",
    "\n",
    "### Complex Conjugate Poles and Residues\n",
    "In Vector Fitting, complex poles $\\underline{p}_k = p'_k + \\mathrm{j} p''_k$ and residues $\\underline{c}_k = c'_k + \\mathrm{j} c''_k$ always come in complex conjugate pairs $(\\underline{p}_k, \\underline{p}_k^*)$ and $(\\underline{c}_k, \\underline{c}_k^*)$. The target response for an equivalent electrical circuit of such a complex conjugate pair is therefore:\n",
    "\n",
    "\\begin{equation}\n",
    "\\underline{H}_\\mathrm{target}(\\mathrm{\\underline{s}}) = \\frac{\\underline{c}_k}{\\mathrm{\\underline{s}} - \\underline{p}_k} + \\frac{\\underline{c}^*_k}{\\mathrm{\\underline{s}} - \\underline{p}^*_k} = \\frac{2 c'_k \\mathrm{\\underline{s}} - 2 (c'_k p'_k + c''_k p''_k)}{\\mathrm{\\underline{s}}^2 - 2 p'_k \\mathrm{\\underline{s}} + |\\underline{p}_k|^2}\n",
    "\\end{equation}\n",
    "\n",
    "There are different ways to build an equivalent circuit out of passive or active components, which can be dimensioned to match this target frequency response. Four such circuits are presented and analyzed in [[3](#link_ref3)], for example a series RLC circuit combined with a parallel current source controlled by the voltage over the capacitor. \n",
    "\n",
    "<img src='./figures/circuit_series_RCL_parallel_current.svg' width='200px'>\n",
    "\n",
    "This circuit is providing an admittance $\\underline{Y}_\\mathrm{RCL,I}(\\mathrm{\\underline{s}})$ that can be matched to the target response:\n",
    "\n",
    "\\begin{equation}\n",
    "\\underline{Y}_\\mathrm{RCL,I}(\\mathrm{\\underline{s}}) = \\frac{1/L \\mathrm{\\underline{s}} + b}{\\mathrm{\\underline{s}}^2 + R/L \\mathrm{\\underline{s}} + 1 / (LC)}\n",
    "\\end{equation}\n",
    "\n",
    "This admittance matches the target response if $L = \\frac{1}{2 c'_k}$, $R = \\frac{-p'_k}{ c'_k}$, $C = \\frac{2 c'_k}{|\\underline{p}_k|^2}$ and $b = -2 (c'_k p'_k + c''_k p''_k)$ with $g_\\mathrm{m} = bLC = \\frac{b}{|\\underline{p}_k|^2}$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Equivalent Circuit of a Vector Fitted $N$-Port\n",
    "### Case 1: Vector Fitted S Parameters\n",
    "The equivalent circuit of a $N$-port with a Vector Fitted S matrix consists of an interface network and a transfer network for each port. The figure below shows the structure of the interface and transfer networks of one such port $i$ with the external port voltage $V_i$ and port current $I_i$. The individual frequency responses $\\underline{S}_{i,j}$ of the Vector Fit are reproduced with the equivalent admittances $\\underline{Y}_{\\mathrm{S},i,j}$ based on the fitting parameters $\\mathbf{\\underline{p}}$, $\\mathbf{\\underline{c}}$, $\\mathbf{d}$ and $\\mathbf{e}$ as described above.\n",
    "\n",
    "<img src='./figures/circuit_equivalent_Nport_S.svg' width='500px'>\n",
    "\n",
    "### Case 2: Vector Fitted Y Parameters\n",
    "Not implemented. See [Y Parameters](https://en.wikipedia.org/wiki/Admittance_parameters) for a general equivalent circuit.\n",
    "\n",
    "### Case 3: Vector Fitted Z Parameters\n",
    "Not implemented. See [Z Parameters](https://en.wikipedia.org/wiki/Impedance_parameters) for a general equivalent circuit."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "<div id='link_ref1'></div>[1] B. Gustavsen and A. Semlyen, \"Rational approximation of frequency domain responses by vector fitting,\" in IEEE Transactions on Power Delivery, vol. 14, no. 3, pp. 1052-1061, July 1999, DOI: [10.1109/61.772353](https://doi.org/10.1109/61.772353).\n",
    "\n",
    "<div id='link_ref2'></div>[2] https://www.sintef.no/projectweb/vectorfitting/\n",
    "\n",
    "<div id='link_ref3'></div>[3] G. Antonini, \"SPICE equivalent circuits of frequency-domain responses,\" in IEEE Transactions on Electromagnetic Compatibility, vol. 45, no. 3, pp. 502-512, Aug. 2003, DOI: [10.1109/TEMC.2003.815528](https://doi.org/10.1109/TEMC.2003.815528)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
